package com.leo.interceptor;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: HandlerInterceptor1
 * @Description: 测试拦截器1
 * @Author: leo825
 * @Date: 2020-02-03 16:05
 * @Version: 1.0
 */
public class HandlerInterceptor1 extends HandlerInterceptorAdapter {

    //获取日志
    private static final Logger LOGGER = LogManager.getLogger();
    
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LOGGER.info("HandlerInterceptor1 preHandle");
        HttpSession session = request.getSession();
        session.setAttribute("startTime",System.currentTimeMillis());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        LOGGER.info("HandlerInterceptor1 postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        LOGGER.info("HandlerInterceptor1 afterCompletion");
        HttpSession session = request.getSession();
        long startTime = (long)session.getAttribute("startTime");
        LOGGER.info("HandlerInterceptor1 过滤的接口耗时：" + (System.currentTimeMillis() - startTime) + "ms");
    }
}
